package com.example.contactstest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.accessibility.AccessibilityManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

//读取系统电话簿中的联系人信息
public class MainActivity extends AppCompatActivity {
    //适配器
    ArrayAdapter<String> adapter;
    // 数据集合
    List<String> contactsList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView contactsView = findViewById(R.id.contacts_view);
        // 创建适配器
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
        // 给控件设置适配器
        contactsView.setAdapter(adapter);
        // 运行时权限的处理逻辑
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            // 申请用户授权操作
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
        }else {
            readContacts();
        }
    }

    private void readContacts() {
        Cursor cursor = null;
        try {
            //查询所有联系人数据
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone
            .CONTENT_URI,null,null,null,null);
            // 取出
            if(cursor!=null){
                while(cursor.moveToNext()){
                    //获取联系人姓名
                    @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    // 获取联系人的电话号码
                    @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // 添加到集合数据
                    contactsList.add(name + "\n" + phone);
                }
                //集合数据更新了，刷新adpter中的 绑定数据
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(cursor != null){
                cursor.close();
            }
        }
    }
    
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    readContacts();
                }else {
                    Toast.makeText(this,"拒接读取电话联系人权限",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}